﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using UnityEngine;
using UnityEngine.UI;

namespace ScFramework.Core
{
    /// <summary>
    /// 程序入口，对各项管理类进行初始化操作。
    /// </summary>
    public class Main:MonoBehaviour
    {
        public string Level;
        public void PopText(string text)
        {
            var tp=ResourceManager.Instance.Instantiate("Prefabs/Pop",Camera.main.transform);
            tp.transform.localPosition = new Vector3(0, 0, 1);
            tp.GetComponent<TextMesh>().text = text;
           

        }
        public CameraConfig CameConf = new CameraConfig();
        public Conf Config = null;
        /// <summary>
        /// 入口流程。
        /// </summary>
        public string StartProcedure;
        public string CurrentGameMode = "GameMode";
        public readonly static string mapSave = "C:\\Users\\santongding\\Documents\\BiggerOrSmaller\\BiggerOrSmaller\\Assets/Resources/Map/";

        private static Main _instance;
        public static Transform Map;
        /// <summary>
        /// 系统组件列表
        /// </summary>
        private List<SystemModule> _modules;

        public static Main Instance
        {
            get => _instance;
        }

        /// <summary>
        /// 获取系统组件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T Get<T>()where T:SystemModule
        {
            foreach(var x in _modules)
            {
                if(x is T)
                {
                    return (T)x;
                }
            }
            Debug.LogError($"{_instance}:未找到组件"+typeof(T).ToString());
            return default(T);
        }
        /// <summary>
        ///  初始化各项全局管理。
        /// </summary>
        private void Awake()
        {
            Map = GameObject.Find("Map").transform;
            if(_instance != null)
            {
                Debug.LogError($"{this}:存在多个实例");
            }
            Config = Resources.Load<Conf>("Config/Conf");
            _instance = this;

            _modules = new List<SystemModule>();

            _modules.Add(new ResourceManager());
            _modules.Add(new TypeManager());
            _modules.Add(new MessageManager());
            _modules.Add(new ProcedureManager());
            _modules.Add(new Data());
            _modules.Add(new InputManager());
            _modules.Add(new CameraManager());

        //    EntityUtilities.InitUseableComponent();
        //    EntityUtilities.InitEntityDefaultSetting();


            for (int i = 0; i < _modules.Count; i++)
            {
                _modules[i].Initialize(this);
                if(_modules[i]is INeedRemove a)
                {
                    needRemoveList.Add(a);
                }
            }

            for (int i = 0; i < _modules.Count; i++)
            {
                _modules[i].Enable();
            }


            GameMode.SetGamemode(Get<TypeManager>().CreatInstanceByShortName<IGameMode>(CurrentGameMode),this);





        }

        List<INeedRemove> needRemoveList = new List<INeedRemove>();
        public void DoRemove(object obj)
        {
            foreach(var x in needRemoveList)
            {
                x.RemoveAbout(obj);
            }
        }
        /// <summary>
        ///  开始流程。
        /// </summary>
        private void Start()
        {
              Get<ProcedureManager>().ChangeTo(StartProcedure);
        }

        /// <summary>
        /// Unity每帧更新。
        /// </summary>
        private void Update()
        {
            deltaTime += (Time.unscaledDeltaTime - deltaTime) * 0.1f;
            for (int i = 0; i < _modules.Count; i++)
            {
                _modules[i].Update(Time.deltaTime);
            }
        }

        /// <summary>
        /// Unity每帧更新。
        /// </summary>
        private void FixedUpdate()
        {
            for (int i = 0; i < _modules.Count; i++)
            {
                _modules[i].FixedUpdate(Time.fixedDeltaTime);
            }
            for(int i = 0; i < _modules.Count; i++)
            {
                _modules[i].LateFixedUpdate();
            }
        }

        /// <summary>
        /// 当退出时调用。
        /// </summary>
        private void OnDestroy()
        {
            for (int i = 0; i < _modules.Count; i++)
            {
                _modules[i].Dispose();
            }
        }
        float deltaTime = 0.0f;

   

        void OnGUI()
        {
            int w = Screen.width, h = Screen.height;

            GUIStyle style = new GUIStyle();

            Rect rect = new Rect(0, 0, w, h * 2 / 100);
            style.alignment = TextAnchor.UpperLeft;
            style.fontSize = h * 2 / 100;
            //new Color (0.0f, 0.0f, 0.5f, 1.0f);
            style.normal.textColor = Color.white;
            float msec = deltaTime * 1000.0f;
            float fps = 1.0f / deltaTime;
            string text = string.Format("{0:0.0} ms ({1:0.} fps)", msec, fps);
            GUI.Label(rect, text, style);
        }
        private void OnDrawGizmos()
        {
            if (Application.isPlaying)
            {
                Gizmos.color = Color.red;
                Gizmos.DrawWireCube(Get<CameraManager>().CameraPos, Get<CameraManager>().ViewSiz*2);
            }
        }

    }
    
    public interface INeedRemove
    {
        void RemoveAbout(object obj);
    }
    
}


public class ObjectArgs<T> : EventArgs
{
    public T obj;
    public ObjectArgs(T x)
    {
        obj = x;
    }
}